<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <el-tab-pane label="登录账户设置">

            <!-- 放置表单 -->
            <el-form ref="formInfo" :model="formData" :rules="rules" label-width="120px" style="margin-left: 120px; margin-top:30px">
              <el-form-item label="姓名:" prop="username">
                <el-input v-model="formData.username" style="width:300px" />
              </el-form-item>
              <el-form-item label="密码:" prop="password2">
                <el-input v-model="formData.password2" style="width:300px" type="password" />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="saveUser">更新</el-button>
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <el-tab-pane label="个人详情">
            <el-row type="flex" justify="end">
              <el-tooltip content="打印个人基本信息">
                <router-link :to="`/employees/print/${userId}?type=personal`">
                  <i class="el-icon-printer" />
                </router-link>
              </el-tooltip>
            </el-row>
            <component :is="userComponent" />
            <!-- <user-info /> -->
          </el-tab-pane>
          <el-tab-pane label="岗位信息">
            <el-row type="flex" justify="end">
              <el-tooltip content="打印岗位信息">
                <router-link :to="`/employees/print/${userId}?type=job`">
                  <i class="el-icon-printer" />
                </router-link>
              </el-tooltip>
            </el-row>
            <job-info />
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getUserDetailById } from '@/api/user'
import { getRequiredRules, getMinMaxRules } from '@/utils/validate'
import { saveUserDetailById } from '@/api/employees'
import JobInfo from './components/job-info.vue'
import userComponent from './components/user-info.vue'
export default {
  name: '',
  components: { JobInfo },
  props: {},
  data() {
    return {
      userComponent: userComponent,
      formData: {
        username: '',
        password2: ''
      },
      rules: {
        username: [getRequiredRules('用户名')],
        password2: [getRequiredRules('用户名'), getMinMaxRules(6, 9, '密码')]
      }
    }
  },
  computed: {
    userId() {
      return this.$route.params.id // 获取数据 后面直接用 this.userId
    }
  },
  watch: {},
  created() {
    this.getUserDetailById()
  },
  mounted() {
    console.log(this.$route)
  },
  methods: {
    async getUserDetailById(id) {
      this.formData = await getUserDetailById(this.userId)
    },
    async saveUser() {
      try {
        // 校验表单
        await this.$refs.formInfo.validate()
        // 调用接口数据
        await saveUserDetailById({
          ...this.formData,
          password: this.formData.password2 // 把新密码赋值给旧密码
        })
        // 提示
        this.$message.success('更新成功')
      } catch (err) {
        console.log(err)
      }
    }
  }
}
</script>

<style scoped>

</style>
